package com.yangming.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yangming.common.base.Page;
import com.yangming.mapper.EmptyCartServiceMapper;
import com.yangming.mapper.SupplyMapper;
import com.yangming.model.EmptyCartService;
import com.yangming.model.Supply;
import com.yangming.service.ISupplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 货源接口实现类
 * @author PengShuiMing
 * @create 2018-09-07
 **/
@Service
public class SupplyServiceImpl implements ISupplyService {
    @Autowired
    private SupplyMapper supplyMapper;
    @Autowired
    private EmptyCartServiceMapper emptyCartServiceMapper;
    /**
     * 新增货源
     * @param supply
     * @return
     */
    @Override
    public int saveSupply(Supply supply) {
        supply.setCreateTime(new Date());
        return supplyMapper.insertSelective(supply);
    }
    /**
     * 查询空板车服务集合
     * @param supply
     * @return
     */
    @Override
    public Map<String,Object> selectEmptyCartServiceList(Supply supply, Page page) {
        //出发地id
        Map<String,Object> params = new HashMap<String,Object>();
        //出发地
        params.put("departureId",supply.getDepartureId());
        //目的地
        params.put("destinationId",supply.getDestinationId());
        //过期时间
        params.put("endTime",new Date());

        //分页查询
        PageHelper.startPage(page.getPageNo(),page.getPageSize());
        List<EmptyCartService> emptyCartServices = emptyCartServiceMapper.MatchingServiceByFilter(params);
        PageInfo<EmptyCartService> pageInfo = new PageInfo<EmptyCartService>(emptyCartServices);

        Map<String,Object> resultMap = new HashMap<String,Object>();
        resultMap.put("data",emptyCartServices);//数据集
        resultMap.put("total",pageInfo.getTotal());//总数据量
        resultMap.put("pageNo",pageInfo.getPageNum());//当前页
        resultMap.put("pageSize",pageInfo.getPageSize());//每页的数量
        resultMap.put("pages",pageInfo.getPages());//总页数
        resultMap.put("hasNetPage",pageInfo.isHasNextPage());//是否有下一页
        return resultMap;
    }
}
